#import "BWCrossStitchSingleLayerTool.h"

#import "BWCrossStitchChangeList.h"
#import "BWLayerSet.h"
#import "BWGridGeometry.h"
#import "BWCrossStitchLayer.h"
#import "BWLayerView.h"

#import "BWColorKeyWindowController.h"

@implementation BWCrossStitchSingleLayerTool


- (void) pickLayer
{
    [layer setChangeList: nil];

    [changeList purge];

    [layer release];
    layer = [self pickLayerForKind: kLayerKind_CrossStitch];
    [layer retain];

    [layer setChangeList: changeList];

} // pickLayer



- (id) initWithGeometry: (BWGridGeometry *) g
                 layers: (BWLayerSet *) l
              layerView: (BWLayerView *) lv
{
    if (self = [super initWithGeometry: g
                      layers: l
                      layerView: lv]) {
        changeList = [[BWCrossStitchChangeList alloc] init];
        [self pickLayer];
    }

    return (self);

} // initWithGeometry


- (void) dealloc
{
    [changeList release];
    [layer release];

    [super dealloc];

} // dealloc



- (unsigned int) capabilities
{
    unsigned int capabilities = [super capabilities];
    
    capabilities |= kCapability_CrossStitch;
    capabilities |= kCapability_TopLayer;
    return (capabilities);

} // capabilities


- (NSArray *) affectedLayers
{
    if (layer == nil) {
        return (nil);
    } else {
        return ([NSArray arrayWithObject: layer]);
    }

} // affectedLayers



- (BOOL) handleMouseDown: (NSEvent *) event
                 atPoint: (NSPoint) mouse
{
    constrain = ([event modifierFlags] & NSShiftKeyMask) ? YES : NO;

    [self pickLayer];
    [layer setChangeList: changeList];

    int row, column;
    [geometry getRow: &row
              andColumn: &column
              atPoint: mouse];

    BWThread *thread = [[BWColorKeyWindowController sharedController]
                           selectedThread];

    [self startDragAtRow: row
          column: column
          thread: thread];

    dragging = YES;

    return (YES);

} // handleMouseDown


- (void) handleMouseDragged: (NSEvent *) event
                    atPoint: (NSPoint) mouse
{
    constrain = ([event modifierFlags] & NSShiftKeyMask) ? YES : NO;

    int row, column;
    [geometry getRow: &row
              andColumn: &column
              atPoint: mouse];

    BWThread *thread = [[BWColorKeyWindowController sharedController]
                           selectedThread];

    [self continueDragAtRow: row
          column: column
          thread: thread];

} // handleMouseDragged


- (void) handleMouseUp: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
    [self finishDrag];

    [layer applyChanges];

    dragging = NO;

} // handleMouseUp


- (void) handleKeyDown: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
    NSString *chars = [event characters];
    unichar character = [chars characterAtIndex: 0];

    if (character == 27) {
        [self abortDrag];
    }

} // handleKeyDown


// the reason for this is for things like line and rectangle, to
// have it snap to constrained mode when the shift key is depressed,
// even if the mouse hasn't moved.

- (void) handleFlagsChanged: (NSEvent *) event
                    atPoint: (NSPoint) mouse
{
    if (dragging) {
        [self handleMouseDragged: event
              atPoint: mouse];
    }

} // handleFlagsChanged


- (BOOL) isConstrained
{
    return (constrain);
} // isConstrained



- (void) startDragAtRow: (int) row  
                 column: (int) column
                 thread: (BWThread *) thread
{
} // startDragAtRow


- (void) continueDragAtRow: (int) row
                    column: (int) gollum
                    thread: (BWThread *) thread
{
} // continueDragAtRow


- (void) finishDrag
{
} // finishDrag


- (void) abortDrag
{
    dragging = NO;
    [layer setChangeList: nil];

} // abortDrag

@end // BWCrossStitchSingleLayerTool

